/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.coupon.api.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.CouponDto;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.coupon.common.model.Coupon;
import com.yami.shop.coupon.common.model.CouponUser;
import com.yami.shop.coupon.common.service.CouponService;
import com.yami.shop.coupon.common.service.CouponUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 我的优惠券接口
 *
 * @author lanhai
 */
@RestController
@RequestMapping("/p/myCoupon")
@Api(tags = "我的优惠券接口")
@AllArgsConstructor
public class MyCouponController {

    private CouponService couponService;
    private CouponUserService couponUserService;

    @GetMapping("/listCouponIds")
    @ApiOperation(value = "查看用户拥有的所有优惠券id", notes = "查看用户拥有的所有优惠券id")
    public ServerResponseEntity<List<CouponDto>> list() {
        String userId = SecurityUtils.getUser().getUserId();
        List<CouponDto> coupons = couponService.listCouponIdsByUserId(userId);
        return ServerResponseEntity.success(coupons);
    }

    @PostMapping("/receive")
    @ApiOperation(value = "领取优惠券接口", notes = "领取优惠券接口")
    @ApiImplicitParam(name = "couponId", value = "优惠券ID", required = true, dataType = "Long")
    public ServerResponseEntity<String> receive(@RequestBody Long couponId) {
        String userId = SecurityUtils.getUser().getUserId();
        Coupon coupon = couponService.getById(couponId);
        couponService.receive(coupon, userId);
        // 领取优惠券成功
        return ServerResponseEntity.success(I18nMessage.getMessage("yami.coupon.receive.success"));
    }

    @GetMapping("/getMyCouponsStatusCount")
    @ApiOperation(value = "获取优惠券个数", notes = "获取各个状态下的优惠券个数")
    public ServerResponseEntity<Map<String, Long>> getCouponCountByStatus() {
        String userId = SecurityUtils.getUser().getUserId();
        Map<String, Long> couponCount = couponService.getCouponCountByStatus(userId);
        return ServerResponseEntity.success(couponCount);
    }

    @GetMapping("/getCouponList")
    @ApiOperation(value = "通过状态查看用户的优惠券列表信息", notes = "通过状态查看用户的优惠券列表信息,优惠券状态 0:已过期 1:未使用 2:使用过")
    @ApiImplicitParam(name = "status", value = "优惠券状态 0:失效 1:有效 2:使用过", required = true, dataType = "Integer")
    public ServerResponseEntity<IPage<CouponDto>> getCouponList(PageParam<CouponDto> page, @RequestParam("status") Integer status) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<CouponDto> couponDtoList = couponService.getCouponListByStatus(page, userId, status);
        return ServerResponseEntity.success(couponDtoList);
    }

    @Deprecated
    @DeleteMapping("/deleteCoupon/{couponId}")
    @ApiOperation(value = "删除用户优惠券", notes = "通过优惠券id删除用户优惠券")
    @ApiImplicitParam(name = "couponId", value = "优惠券Id", required = true, dataType = "Long")
    public ServerResponseEntity<String> deleteCoupon(@PathVariable("couponId") Long couponId) {
        String userId = SecurityUtils.getUser().getUserId();
        couponService.deleteUserCouponByCouponId(userId, couponId);
        // 删除成功
        return ServerResponseEntity.success(I18nMessage.getMessage("yami.delete.successfully"));
    }

    @DeleteMapping("/delCoupon/{couponUserId}")
    @ApiOperation(value = "删除用户优惠券", notes = "通过优惠券关联id删除用户优惠券")
    @ApiImplicitParam(name = "couponUserId", value = "优惠券关联id", required = true, dataType = "Long")
    public ServerResponseEntity<String> deleteCouponById(@PathVariable("couponUserId") Long couponUserId) {
        String userId = SecurityUtils.getUser().getUserId();
        CouponUser couponUser = new CouponUser();
        couponUser.setCouponUserId(couponUserId);
        couponUser.setIsDelete(1);
        couponUserService.update(couponUser, new LambdaUpdateWrapper<CouponUser>()
                .eq(CouponUser::getCouponUserId, couponUserId).eq(CouponUser::getUserId, userId));
        // 删除成功
        return ServerResponseEntity.success(I18nMessage.getMessage("yami.delete.successfully"));
    }

    @GetMapping("/generalCouponList")
    @ApiOperation(value = "通用券列表(平台优惠券)", notes = "获取通用券列表")
    public ServerResponseEntity<List<CouponDto>> generalCouponList() {
        String userId = SecurityUtils.getUser().getUserId();
        List<CouponDto> couponList = couponService.generalCouponList(userId);
        return ServerResponseEntity.success(couponList);
    }

//    @GetMapping("/prodCouponList")
//    @ApiOperation(value = "商品券列表", notes = "获取商品券列表(指定商品可用优惠券)")
//    public ServerResponseEntity<IPage<CouponDto>> prodCouponList(PageParam<CouponDto> page) {
//        String userId = SecurityUtils.getUser().getUserId();
//        return ServerResponseEntity.success(couponService.pageProdCoupon(page, userId));
//    }

    @GetMapping("/getCouponPage")
    @ApiOperation(value = "商品券列表(商家优惠券)", notes = "获取商品券列表(指定商品可用优惠券)")
    public ServerResponseEntity<IPage<CouponDto>> getCouponList(PageParam<CouponDto> page) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<CouponDto> couponDto = couponService.getCouponList(page, userId);
        return ServerResponseEntity.success(couponDto);
    }
}
